package com.gp.learn.ssm.common.utils;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.gp.learn.ssm.base.core.mybaits.Entity.EntityMap;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.*;

/**
 * 工具类
 */
public class FlymeUtils {

    /**
     * 其中一个为空
     *
     * @param objs
     * @return
     */
    public static Boolean anyOneIsNull(Object... objs) {
        int length = objs.length;
        for (int i = 0; i < length; i++) {
            Object obj = objs[i];
            if (isEmpty(obj)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 都为空
     *
     * @param objs
     * @return
     */
    public static boolean allIsNull(Object... objs) {
        int n = objs.length;
        int t = 0;
        for (int i = 0; i < n; ++i) {
            Object o = objs[i];
            if (isEmpty(o)) {
                t++;
            }
        }
        if (t == n) {
            return true;
        }
        return false;
    }

    /**
     * List转换为数组T[] a
     */
    public static <T> Long[] listToArray(List<T> list) {
        return  list.toArray(new Long[list.size()]);
    }

    /**
     * 都不为空
     *
     * @param obj
     * @return
     */
    public static boolean allNotNull(Object... obj) {
        return !anyOneIsNull(obj);
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 以UUID重命名
     *
     * @param fileName
     * @return
     */
    public static String renamePic(String fileName) {
        String extName = fileName.substring(fileName.lastIndexOf("."));
        return UUID.randomUUID().toString().replace("-", "") + extName;
    }

    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        } else if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        } else if (obj instanceof CharSequence) {
            return ((CharSequence) obj).length() == 0;
        } else if (obj instanceof String) {
            return ((String) obj).trim().equals("");
        } else if (obj instanceof Collection) {
            return ((Collection) obj).isEmpty();
        } else {
            return obj instanceof Map ? ((Map) obj).isEmpty() : false;
        }
    }


    /**
     *
     */
    public static BigDecimal gertBigDecimal(BigDecimal v, String defval) {
        if (ObjectUtils.isEmpty(v)) {
            return new BigDecimal(defval);
        }
        return v;
    }


    /**
     *
     */
    public static String getString(String v, String defval) {
        if (isEmpty(v)) {
            return defval;
        }
        return v;
    }

    public static Long getLong(Object v) {
        if (ObjectUtils.isEmpty(v)) {
            return null;
        }
        return Long.parseLong(v.toString());
    }


    /**
     *
     */
    public static Integer getInteger(Integer v, Integer defval) {
        if (ObjectUtils.isEmpty(v)) {
            return defval;
        }
        return v;
    }

    /**
     *
     */
    public static List<String> stringTolist(String str) {
        List<String> list = new ArrayList<String>();
        if (ObjectUtils.isNotEmpty(str)) {
            String[] temp = str.split(",");
            list = Arrays.asList(temp);
        }
        return list;
    }

    public static String listToString(List<String> stringList) {
        if (ObjectUtils.isNotEmpty(stringList)) {
            return StringUtils.join(stringList, ",");
        } else {
            return null;
        }
    }

    /**
     * 中文數字转阿拉伯数组【十万九千零六十  --> 109060】
     */
    @SuppressWarnings("unused")
    public static String chineseNumber2Int(String chineseNumber) {
        String[] s1 = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        String[] s2 = {"十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"};
        String result = "";
        int n = chineseNumber.length();
        for (int i = 0; i < n; i++) {
            int num = chineseNumber.charAt(i) - '0';
            if (i != n - 1 && num != 0) {
                result += s1[num] + s2[n - 2 - i];
            } else {
                result += s1[num];
            }
            System.out.println("  " + result);
        }
        return result;
    }


    public static <T> T getEntity(Class<T> t, EntityMap requestMap) {
        if (FlymeUtils.isNotEmpty(requestMap)) {
            Map<Object, Object> map = new HashMap<>();
            map.putAll(requestMap);
            return JsonUtils.jsonToBean(JSONUtil.toJsonStr(map), t);
        } else {
            return null;
        }

    }

    public static String getAlias(String tableName) {
        String[] str = tableName.split("_");
        String alias = "";
        if (str.length > 2) {
            for (String s : str) {
                alias += s.substring(0, 1);
            }
        } else {
            alias = str[str.length - 1];
        }
        return alias;
    }

    /**
     * v1<v2
     */
    public static Boolean lt(BigDecimal v1, BigDecimal v2) {
        Boolean tag = false;
        if (v1.compareTo(v2) == -1) {
            tag = true;
        }
        return tag;
    }

    /**
     * 判断BigDecimal是否小于等于0
     */
    public static Boolean le(BigDecimal v1, BigDecimal v2) {
        Boolean tag = false;
        if (v1.compareTo(v2) == -1) {
            tag = true;
        }
        if (v1.compareTo(v2) == 0) {
            tag = true;
        }
        return tag;
    }

    public static Long[] StringArray2LongArray(String[] stringArray) {
        List<Long> list = new ArrayList<>();
        for (String str : stringArray) {
            try {
                list.add(Long.parseLong(str));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        Long[] longArray = list.toArray(new Long[list.size()]);
        return longArray;
    }


    public static Boolean contains(Object[] objs, Object obj) {
        return Arrays.asList(objs).contains(obj);
    }

    public static void main(String[] args) {
        try {
            File basePath = new File(ResourceUtils.getURL("classpath:").getPath());
            System.out.println(basePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

}
